import Koa from "koa";
import serve from "koa-static";
import path from "path";
import bodyParser from "koa-bodyparser";
import apiRouter from "./routes/api.js";
import testRouter from "./routes/test.js";

const app = new Koa();

// 添加 body 解析中间件
app.use(bodyParser());

// 静态文件服务中间件
const __dirname = path.dirname(new URL(import.meta.url).pathname);
const staticDir = path.join(__dirname, "./public");
app.use(serve(staticDir));

// 使用 API 路由中间件，并为其设置前缀
apiRouter.prefix("/api");
app.use(apiRouter.routes()).use(apiRouter.allowedMethods());
// 使用测试路由中间件，并为其设置前缀
testRouter.prefix("/test");
app.use(testRouter.routes()).use(testRouter.allowedMethods());

export default app;
